package com.gyg.controller;

import com.gyg.common.lang.Result;
import com.gyg.entity.File;
import com.gyg.service.IFileService;
import com.gyg.util.FTPUtil;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Objects;

/**
 * <des>
 *
 * <des>
 *
 * @create Date 2021-11-03
 **/
@RestController
public class FileController {
    private static final Logger log = LoggerFactory.getLogger(FileController.class);

    @Autowired
    IFileService iFileService;


    @RequestMapping("/file/list")
    public Result list(){

        return Result.success(iFileService.list());
    }

    @RequestMapping("/file/save")
    public Result save(File file){

        return Result.success(iFileService.save(file));
    }

    @RequestMapping("/file/update")
    public Result update(@RequestBody File file){
        return Result.success(iFileService.update(file));
    }

    @RequestMapping("/file/delete")
    public Result delete(Integer id){
        return Result.success(iFileService.delete(id));
    }

    @RequiresAuthentication
    @RequestMapping(value = "/file/getFileUploadList",method = RequestMethod.POST)
    public Result getFileUploadList() throws IOException {
        return Result.success(iFileService.list());
    }

    @RequiresAuthentication
    @RequestMapping(value = "/file/upload",method = RequestMethod.POST)
    public Result fileUpload(MultipartFile file) throws IOException {
        log.debug("传入的文件参数：{}",file.toString());
        if (Objects.isNull(file) || file.isEmpty()) {
            log.error("文件为空");
            return Result.fail("文件为空，请重新上传");
        }
        String hostname = "192.168.123.96";
        int port = 12333;
        String username = "pc";
        String password = "123456";
        String workingPath = "/upload";
        InputStream inputStream = new ByteArrayInputStream(file.getBytes());
        String saveName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+"_"+file.getOriginalFilename();
        String path ="E:\\FTPServer\\upload\\"+saveName;
        FTPUtil.upload(hostname, port, username, password, workingPath, inputStream, saveName);
        File file1= new File();
        file1.setFileName(saveName);
        file1.setCreated(LocalDateTime.now());
        file1.setUrl("ftp://192.168.123.96:12333/upload"+saveName);
        iFileService.save(file1);
        return Result.success(path);
    }

}
